package com.neca.auction.pages;

import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.ioc.annotations.Inject;

import com.neca.auction.dbb.DBBroker;
import com.neca.auction.model.CurrentUser;
import com.neca.auction.model.User;

public class Register {
	@ApplicationState
	private CurrentUser currentUser;
	@Persist
	private String firstName;
	@Persist
	private String lastName;
	@Persist
	private String userName;
	@Persist
	private String password;
	
	private String password2;
	@Persist
	private String phone;
	@Persist
	private String mail;
	@Component
	private Form registrationForm;
	@Inject 
	private Messages messages;
	
	
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword2() {
		return password2;
	}
	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
	
	void onSubmitFromRegistrationForm(){
		System.out.println("Form was submitted!!!");
		
		
	}
	void onValidate(){
		if(password.equals(password2)){
			currentUser=new CurrentUser(userName);
			System.out.println(currentUser.getUserName());
		}
		else{
			registrationForm.recordError(messages.get("passwor-match"));
		}
	}
	
	Object onSuccess(){
		User user=new User(firstName, lastName, userName, password, phone, mail, 1);
		DBBroker dbbroker=DBBroker.getDbbroker();
		dbbroker.addUser(user);
		return MainNavigation.class;
	}
	
	
	
	

}
